<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>即时分配</title>
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />

		<!-- 使用struts 2的 datetimepicher 必须有这个头文件 -->
		<s:head />
		<link href="<%=request.getContextPath()%>/css/tabcard.css"
			rel="stylesheet" type="text/css">
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/style.css" type="text/css" />
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/studentAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/apartmentViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/studentLodgeMgrAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dormitoryAllocateAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type='text/javascript'
			src="<%=request.getContextPath()%>/js/tabcard.js"></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/commonValidate.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/sorttable.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
		<script type='text/javascript'>
			$j(function(){
				tbCSS();
				$('student_basic_info_tab').style.display = 'none';
				$('btnAssign').style.display = 'none';
				$('startt').style.display = 'none';
				$('endt').style.display = 'none';
			})
		</script>

		<script type='text/javascript'>
/*
*包涵该页面
*给$('student_info').value赋值
*调用stu_BasicInfo()
*/



var stuno_bs;   //bs = basic student.  学号
//属于tabcard.js里的方法，在单击tab时触发
function mSearch(obj){
	if(stuno_bs != null){
		switch(obj.id){
			case "basicInfo_bs" : studentAction.getStudentBasicInfo(stuno_bs,stu_BasicInfoCallback);break;
			case "dormHistory_bs" : studentAction.getStudentHistoryAccomodationInfo(stuno_bs,stu_HistoryAccomodationInfoCallback);break;
			case "chargeInfo_bs" : studentAction.getStudentChargeRecord(stuno_bs,getStudentChargeRecordCallback);break;
			case "dormRate_bs": searchDormitoryRateStatus();break;
		}
	}
	
}
//验证输入是否有效
function validate_bs(){
	stuno_bs = strTrim_cv($('student_info').value);
	if(stuno_bs == ""){
			alert("请输入学号！");
			return false;
	}
	if(!cnChar_cv(stuno_bs)){
			alert("您只能输入数字,字母和汉字,请重新输入");
			return false;
	}
	return true;
}
//搜索学生信息
//对外的接口方法
function stu_BasicInfo(){
	
	var b = validate_bs();
	if(false == b){
		return ;
	}
	//mSearch()本是tabcard.js中调用的方法,这里调用它,判断当前激活的tab,并触发相关的方法
	mSearch($j('.active').get(0));
}
var stu_BasicInfoCallback = function stu_BasicInfoCallbackFunc(stulist){
	$('student_basic_info_tab').style.display = '';
	$('allocateoneperson_dorm_search').style.display = '';
	$('btnAssign').style.display = '';
	$('startt').style.display = '';
	$('endt').style.display = '';
	if(stulist == null){
		$('student_basic_info_tab').style.display = 'none';
		$('allocateoneperson_dorm_search').style.display = 'none';
		$('startt').style.display = 'none';
		$('endt').style.display = 'none';
		$('btnAssign').style.display = 'none';
		alert("学号不存在！");
		return;
	}
	//alert(stulist.length+"====="+stulist);
	var slist = stulist;
	if($('studentno') != null){
		$('studentno').value = slist[1];
	}
	$('student_id').innerHTML = slist[0]; //加上了隐藏的学生id
	$('stuNo').innerHTML = slist[1];
	$('stuName').innerHTML = slist[2];
	$('stuGender').innerHTML = slist[3];
	$('stuCollege').innerHTML = slist[4];
	$('stuMajor').innerHTML = slist[5];
	$('stuClass').innerHTML = slist[6];
	$('stuDorm').innerHTML = slist[7];
	$('stuPhone').innerHTML = slist[8];
	$('studentpicture').src="/dms"+slist[9];
	$('stuEthnic').innerHTML = slist[10];
	$('stuBirthplace').innerHTML = slist[11];
	$('stuBirthday').innerHTML = slist[12];
	$('stuType').innerHTML = slist[13];
	$('student_preassignid').innerHTML = slist[14]; 
	//alert(slist[14]);
	getAvailDormbyPreId();
	
}
//返回的historyAccomodationRecordList是二维数组
var stu_HistoryAccomodationInfoCallback=function stu_HistoryAccomodationInfoCallbackFunc(historyAccomodationRecordList){
	
	//删除为查询历史住宿信息生成的动态表格
	//表格有一个头部,所以第一行不删
	for(var i=$('student_accomodation_history').rows.length-1;i>0;i--){ 
       	$('student_accomodation_history').deleteRow(i); 
   	} 
   	//判断历史住宿信息是否为空
   	if(null == historyAccomodationRecordList){
   		return;
   	}
   	//动态添加新的记录行
   	var oneHistoryAccomodationRecord;
   	//alert(historyAccomodationRecordList);
   	//oneHistoryAccomodationRecord =  eval('('+historyAccomodationRecordList+')');
	for(var n=0;n<historyAccomodationRecordList.length;n++){
		oneHistoryAccomodationRecord = historyAccomodationRecordList[n];
		//表格有一个头部,所以后面插入的行号要加一
		myrow=$("student_accomodation_history").insertRow(n+1);	
		myrow.id= "tr"+eval(n+1);
		c=myrow.insertCell(0);
		c.innerHTML=oneHistoryAccomodationRecord[2];
		c=myrow.insertCell(1);
		c.innerHTML=oneHistoryAccomodationRecord[0];
		c=myrow.insertCell(2);
		c.innerHTML=oneHistoryAccomodationRecord[1];
	}
}

var getStudentChargeRecordCallback = function getStudentChargeRecordCallbackFunc(list){
	//删除为查询历史交费生成的动态表格
	//表格有一个头部,所以第一行不删
	for(var i=$('student_charge_history').rows.length-1;i>0;i--){ 
       	$('student_charge_history').deleteRow(i); 
   	} 
	
   	//动态添加新的记录行
   	var oneHistoryChargeRecord;
   	if(null == list){
   		return;
   	}
   	//alert(historyAccomodationRecordList);
   	//oneHistoryAccomodationRecord =  eval('('+historyAccomodationRecordList+')');
	for(var n=0;n<list.length;n++){
		oneHistoryChargeRecord = list[n];
		//表格有一个头部,所以后面插入的行号要加一
		myrow=$("student_charge_history").insertRow(n+1);	
		myrow.id= "tr"+eval(n+1);
		c=myrow.insertCell(0);
		c.innerHTML=oneHistoryChargeRecord[2];
		c=myrow.insertCell(1);
		c.innerHTML=(oneHistoryChargeRecord[4]== 1)?"已交费":"未交费";
		c=myrow.insertCell(2);
		c.innerHTML=oneHistoryChargeRecord[3];
	}
}
//enter键按下提交搜索

  function   subEv(event)   {  
	  event = event ? event : (window.event ? window.event : null);    
	  if(event.keyCode==13) {
	  	 stu_BasicInfo();
	  }
  } 
/*
  document.onkeydown=subEv;
*/
$j(function (){
	$j(document).bind("keydown",subEv);
})

function searchDormitoryRateStatus(){
	if(stuno_bs != null && stuno_bs != ""){
		dormitoryStatusViewAction.getDormitoryRateStatus(stuno_bs,searchDormitoryRateStatusCallback);
	}
}
var searchDormitoryRateStatusCallback = function searchDormitoryRateStatusCallbackFunc(list){
	//删除为查询宿舍卫生状况生成的动态表格
	//表格有一个头部,所以第一行不删
	for(var i=$('student_dorm_hygienic').rows.length-1;i>0;i--){ 
       	$('student_dorm_hygienic').deleteRow(i); 
   	} 
   	//判断宿舍卫生状况记录是否为空
   	if(null == list){
   		return;
   	}
	for(var n=0;n<list.length;n++){
		l = list[n];
		//表格有一个头部,所以后面插入的行号要加一
		myrow=$("student_dorm_hygienic").insertRow(n+1);	
		myrow.id= "tr"+eval(n+1);
		c=myrow.insertCell(0);
		c.innerHTML=l[3];
		c=myrow.insertCell(1);
		c.innerHTML=l[2];
		c=myrow.insertCell(2);
		c.innerHTML=l[4];
	}
}	
</script>
		<script type="text/javascript">
		//定义全局变量
		var aprt_id = ''; //公寓id
		var gdorm_id=''; //宿舍id
		var page;   //dwr分页的page对象，注意在页面中是一个Json对象
		var dormCache = { }; //存放宿舍的一个复制
		var preid;
		var totalcount;
	

	//根据preAssignedId获得一个宿舍列表
	function getAvailDormbyPreId(){
		preid = $("student_preassignid").innerHTML; //preid必须为全局变量
		//alert("学生或宿舍的preid为："+preid);
		if(preid==undefined||preid==""){
			alert("系统忙，请稍候！"); //预分配标记字段怎么取不到了？
			return;
		}
		studentLodgeMgrAction.getTotalAvailPreIdDormRows(preid,totalrowsback)
	}

	var totalrowsback = function totalrowsback(totalrows){
		totalcount = totalrows; //总行数赋值
		studentLodgeMgrAction.getPreassignDateByPreID(preid,getPreassignDateByPreIDCallback);
		studentLodgeMgrAction.getAvailDormPerPageByPreID(1,totalcount,preid,mygetAllAvailDormback);
	}
	//根据预分配标记获得起始和结束时间,并给控件赋值
	var getPreassignDateByPreIDCallback = function(dates){
		dojo.widget.byId("startInDate").setValue(dates[0]);
		dojo.widget.byId("endOutDate").setValue(dates[1]);
	}
	//获得一个可用学生宿舍列，有分页的返回
	var mygetAllAvailDormback = function setAllDormList(dlist){
		if(dlist==null){
			$('allocateoneperson_dorm_search').style.display = 'none';
			$('startt').style.display = 'none';
			$('endt').style.display = 'none';
			$('btnAssign').style.display = 'none';
			alert("查询可用宿舍结果为空！");
			return;
		}
		//对按照公寓和宿舍名字查询结果div做处理
		$("selectdormselect").style.display="none"; 
		$("ifDorm").checked==false; //干掉check
		
		if($('dataTable').style.visibility == 'hidden'){
			$('dataTable').style.visibility = 'visible';
		}
		if($('pageTable').style.visibility == 'hidden'){
			$('pageTable').style.visibility = 'visible';
		}
		/**
		*filter，这段不好理解，作用是拷贝了一份dlist，并与服务器的dlist同步id
		*/
		DWRUtil.removeAllRows('ttb', { filter:function(tr) {
		      return (tr.id != "pattern");
			    }});
		      ////===============fengexian====
		 var dorm, id;
		 //alert("#"+dlist[0]+"#");
		 for (var i =0; i < dlist.length ; i++) {
		    dorm = dlist[i];
		    id = dorm[0];
		    dwr.util.cloneNode("pattern", { idSuffix:id });
		    dwr.util.setValue("tableid" + id, dorm[0]);
		    dwr.util.setValue("tablename" + id, dorm[1]);
		     if(dorm[4]==0){
		      dwr.util.setValue("tablesex" + id, "女");
		     }else{
		       dwr.util.setValue("tablesex" + id, "男");
		     }
		      
		      dwr.util.setValue("tableavailno" + id, dorm[3]);
		     dwr.util.setValue("tableroomtype" + id, dorm[2]);
		      dwr.util.setValue("cbx" + id, dorm.dormitoryid);
		   	 $("pattern" + id).style.display= "";
             dormCache[id] = dlist;
             
             //让第一个radio默认选中
             if(i == 0){
             	$('cbx'+id).checked="checked";
             	gdorm_id =$('cbx'+id).id.substring(3);
             	//alert(gdorm_id);
             }
		   }
			setPageInfo();
			tbCSS();
		}

	/**
	* 可用宿舍列表的checkox选中是干的事情
	*/
	function test(eleid){
		//得到的eleid为"cbx{id}"
		  gdorm_id =eleid.substring(3);
		  //alert(gdorm_id);
		}
	
	function editClicked(eleid) {
		  //得到的eleid为"edit{id}"，如 "edit42".,经过substring后为42,person的id
		  alert(eleid.substring(4));
		}
	
	function setPageInfo(){
        $('pageTd').innerHTML = '';
        studentLodgeMgrAction.getPage({     
            callback:function(data){
                page = data;
                if(page.totalPage != 0){
                    var pageSpan = document.createElement('span');
                    pageSpan.innerHTML = '共 '+totalcount+' 条 '+'   第 '+page.currentPage+' 页    共'+page.totalPage+' 页 ';
                    $('pageTd').appendChild(pageSpan);
                    var aFirst = document.createElement('a');
                    aFirst.setAttribute('href','javascript:getFirstData()');
                    aFirst.innerHTML = '首页';
                    $('pageTd').appendChild(aFirst);
                    var aPrevious = document.createElement('a');
                    aPrevious.setAttribute('href','javascript:getPreviousData()');
                    aPrevious.innerHTML = '上一页 ';
                    $('pageTd').appendChild(aPrevious);
                    var aNext = document.createElement('a');
                    aNext.setAttribute('href','javascript:getNextData()');
                    aNext.innerHTML = '下一页 ';
                    $('pageTd').appendChild(aNext);
                    var aLast = document.createElement('a');
                    aLast.setAttribute('href','javascript:getLastData()');
                    aLast.innerHTML = '尾页';
                    $('pageTd').appendChild(aLast);    
                }               
            }
        });
        $('loading').style.display = 'none';
    }    


	function getFirstData(){
		page.currentPage = 1;
		getDataPerPage();
	}
	
	function getPreviousData(){
        if(page.currentPage>1){
            page.currentPage--;
        }
        getDataPerPage();
    }
    
    function getNextData(){
        if(page.currentPage<page.totalPage){
            page.currentPage++;
        }
        getDataPerPage();
    }
	
	function getLastData(){
		page.currentPage = page.totalPage;
		getDataPerPage();
	}
	
	function getDataPerPage(){
		$('loading').style.display = 'block';
		studentLodgeMgrAction.getAvailDormPerPageByPreID(page.currentPage,totalcount,preid,mygetAllAvailDormback);
	}	
	
	//点击分配按钮进行分配
	function submitAllocate() {
	var studentidno = $("student_id").innerHTML;
	if(studentidno==""){
		alert('请选择学生');
		return;
		}else if(($("ifDorm").checked==false)&&(gdorm_id=="")){
		alert('请选择公寓');
		return;
		}else{
			//s:datetimepicker的取值方法
			var startindate= dojo.widget.byId("startInDate").getValue();
			var endoutdate= dojo.widget.byId("endOutDate").getValue();
			var dormidno;
			if(gdorm_id==""){
				dormidno = $("dorm_id").innerHTML;
			}else{
				dormidno = gdorm_id;
				//alert("dormidno  is==="+dormidno)
				}
				if(startindate==""){
					alert("请选择开始入住时间");
					}else if(endoutdate==""){
						alert("请选择结束入住时间");
						}else{			
							//给人安排住宿啦！
							dormitoryAllocateAction.allocateTEMP(studentidno,dormidno,startindate,endoutdate,myallocatedback);
						}
		}
	}
	//分配的callback，返回成功与否
	var myallocatedback = function setStatus(status){
			alert(status);
			stu_BasicInfo();
			$('student_info').select();
		}
</script>
	</head>
	<body>
		<div id="page_container" style="height: auto;">
			<%@   include file="allocatebanner.jsp"%>
			<h2>
				即时分配宿舍
			</h2>

			<div id="student_basic_info">
				<div id="student_basic_info_search">
					请输入学号：
					<input name="textfield" type="text" id="student_info" />
					<input class="common_button" type="submit" name="button"
						id="button" value="搜索" onclick="stu_BasicInfo()" />
				</div>
				<div class="tab_card" id="student_basic_info_tab">
					<ul class="menu">
						<li class="active" id="basicInfo_bs">
							基本信息
						</li>
						<li id="dormHistory_bs">
							住宿历史
						</li>
						<li id="chargeInfo_bs">
							交费信息
						</li>
						<li id="dormRate_bs">
							卫生评比
						</li>
					</ul>
					<div class="tab_card_con">
						<div id="student_basic_info_content2">
							<table class="table_student_basic_info">
								<tbody>
									<tr>
										<td rowspan="7">
											<img id="studentpicture"
												src="<%=request.getContextPath()%>/images/nobody.jpg"
												width="90px" height="120px" alt="头像" />
										</td>
										<td class="st_td_left">
											学号
										</td>
										<td class="st_td_right_1">
											<!-- 学生id的隐藏在这 -->
											<div id="student_id" style="display: none"></div>
											<!-- 学生预分配id的隐藏在这 -->
											<div id="student_preassignid" style="display: none"></div>
											<div id="stuNo"></div>
										</td>
										<td class="st_td_left">
											学院
										</td>
										<td class="st_td_right">
											<div id="stuCollege"></div>
										</td>
									</tr>
									<tr>
										<td class="st_td_left">
											姓名
										</td>
										<td class="st_td_right_1">
											<div id="stuName"></div>
										</td>
										<td class="st_td_left">
											专业
										</td>
										<td class="st_td_right">
											<div id="stuMajor"></div>
										</td>
									</tr>
									<tr>

										<td class="st_td_left">
											性别
										</td>
										<td class="st_td_right_1">
											<div id="stuGender"></div>
										</td>
										<td class="st_td_left">
											班级
										</td>
										<td class="st_td_right">
											<div id="stuClass"></div>
										</td>
									</tr>
									<tr>
										<td class="st_td_left">
											民族
										</td>
										<td class="st_td_right_1">
											<div id="stuEthnic"></div>
										</td>
										<td class="st_td_left">
											类别
										</td>
										<td class="st_td_right">
											<div id="stuType"></div>
										</td>
									</tr>
									<tr>
										<td class="st_td_left">
											籍贯
										</td>
										<td class="st_td_right_1">
											<div id="stuBirthplace"></div>
										</td>
										<td class="st_td_left">
											宿舍
										</td>
										<td class="st_td_right">
											<div id="stuDorm"></div>
										</td>

									</tr>
									<tr>
										<td class="st_td_left">
											生日
										</td>
										<td class="st_td_right_1">
											<div id="stuBirthday"></div>
										</td>
										<td class="st_td_left">
											手机
										</td>
										<td class="st_td_right">
											<div id="stuPhone"></div>
										</td>
									</tr>
								</tbody>
							</table>
						</div>
						<div id="student_basic_info_content3">
							<table id="student_accomodation_history"
								class="table_student_basic_info">
								<tr>
									<td>
										宿舍号
									</td>
									<td>
										开始入住时间
									</td>
									<td>
										结束入住时间
									</td>
								</tr>
							</table>
						</div>
						<div id="student_basic_info_content4">
							<table id="student_charge_history"
								class="table_student_basic_info">
								<tr>
									<td>
										交费时间
									</td>
									<td>
										交费状态
									</td>
									<td>
										交费金额
									</td>
								</tr>
							</table>
						</div>
						<div id="student_basic_info_content5">
							<table id="student_dorm_hygienic"
								class="table_student_basic_info">
								<tr>
									<td>
										评分时间
									</td>
									<td>
										分数
									</td>
									<td>
										评分人
									</td>
								</tr>
							</table>
						</div>
					</div>
				</div>
			</div>
			<script language="javascript">
            
            /**
             *
             * @param string tabId			当前标签组id
             * @param string fromContendId	要加载的内容的id前缀
             * @param int bindType	绑定方式 0为绑定鼠标移上效果 1为鼠标点击的效果
             * @version 1.0.0
             */
            //TabCard(tabId,fromContendId,bindType)
            var tmpObj1 = new TabCard("student_basic_info_tab", "student_basic_info_content", 1);
            tmpObj1.mActiveClass = "active";
            tmpObj1.mNoActiveClass = "default";
            tmpObj1.mListStartNumb = 2;
            tmpObj1.initab();
		</script>
			<br />

			<div id="allocateoneperson_dorm_search">
				<!-- 预分配标记公寓查询结果显示 -->
				<div id="selectdormselect">
					<table>
						<tr>
							<td>
								<div id="chk_dormid" style="display: none">
									<input type="radio" name="ifDorm" id="ifDorm">
								</div>
								<div id="dorm_id" style="display: none"></div>
							</td>
							<td>
								<div id="dorm_name"></div>
							</td>
							<td>
								<div id="dorm_roomtype"></div>
							</td>
						</tr>
					</table>
				</div>
				<span id="tableid" style="display: none">id</span>
				<table id="dataTable" style="visibility: hidden;"
					class="table_common table_list_style sortable"  rowclass="row_odd,row_even" width="600px;">
					<thead>
						<tr>
							<th>
								选择
							</th>
							<th>
								宿舍名
							</th>
							<th>
								住宿性别
							</th>
							<th>
								房型
							</th>
							<th>
								可住人数
							</th>
						</tr>
					</thead>
					<tbody id="ttb">
						<tr id="pattern" style="display: none;">
							<td>
								<input type="radio" id="cbx" name="cbx" onclick="test(this.id)" />
							</td>
							<td>
								<span id="tablename" >name</span>
							</td>
							<td>
								<span id="tablesex" >sex</span>
							</td>
							<td>
								<span id="tableroomtype" >roomtype</span>
							</td>
							<td>
								<span id="tableavailno" >availno</span>
							</td>
						</tr>
					</tbody>
				</table>
				<table id="pageTable"
					style="visibility: hidden; margin-top: 30px; font: 12px;"
					class="navigator">
					<tr>
						<td id="pageTd">
						</td>
					</tr>
				</table>
				<div id="loading"
					style="display: none; margin-top: 20px; text-align: center; color: red; font: 13px;">
					Loading...
				</div>
				<!-- 预分配标记公寓查询结果显示结束 -->
			</div>
			<!-- allocateoneperson_dorm_search -->

			<!-- 入住时间table开始 -->
			<div id="startt">
				开始入住时间：
				<s:datetimepicker name="startInDate" id="startInDate"
					value="today" displayFormat="yyyy-MM-dd" />
			</div>

			<div id="endt">
				结束入住时间：
				<s:datetimepicker name="endOutDate" id="endOutDate"
					displayFormat="yyyy-MM-dd" />
			</div>
			<!-- 入住时间table结束 -->

			<div id="outlier"></div>
			<div id="btnAssign">
				<input class="common_button" type="submit" value=分配
					onclick="javascript:submitAllocate()" />
			</div>
		</div>
	</body>
</html>
